<template>
  <view class="commonLim-list">
    <!-- 表单内容 -->
    <view class="form-content flexandauto">
      <view class="form-item">
        <view>
          <text class="label" style="color: red">*</text>
          <text class="label">随工单编号:</text>
        </view>
        <view class="flexCenter" style="flex: 1">
          <input
            class="iptClass textright"
            placeholder="输入或扫分轮二维码"
            v-model.trim="profile.trackingNo"
            style="width: 100%"
            @blur="handleblur1"
            :disabled="isEdit"
          />
          <image
            src="../../static/tabs/qr.png"
            @click="handlecan1"
            class="qrClass mgl10px"
            v-if="!isEdit"
          />
        </view>
      </view>
      <view class="form-item">
        <view>
          <text class="label">工单编号:</text>
        </view>
        <view>
          <text v-if="profile.workOrderCode" class="iptClass">{{
            profile.workOrderCode
          }}</text>
          <text class="dianwo" v-else>根据分轮编号带出</text>
        </view>
      </view>
      <view class="form-item">
        <view>
          <text class="label">物料编码:</text>
        </view>
        <view>
          <text v-if="profile.materialCode" class="iptClass">{{
            profile.materialCode
          }}</text>
          <text class="dianwo" v-else>根据分轮编号带出</text>
        </view>
      </view>
      <view class="form-item">
        <view>
          <text class="label">材质:</text>
        </view>
        <view>
          <text v-if="profile.materialTypeName" class="iptClass">{{
            profile.materialTypeName
          }}</text>
          <text class="dianwo" v-else>根据分轮编号带出</text>
        </view>
      </view>
      <view class="form-item">
        <view>
          <text class="label">规格型号:</text>
        </view>
        <view>
          <text v-if="profile.specificationName" class="iptClass">{{
            profile.specificationName
          }}</text>
          <text class="dianwo" v-else>根据分轮编号带出</text>
        </view>
      </view>
      <view class="form-item">
        <view>
          <text class="label">炉号:</text>
        </view>
        <view>
          <text v-if="profile.heatNo" class="iptClass">{{
            profile.heatNo
          }}</text>
          <text class="dianwo" v-else>根据分轮编号带出</text>
        </view>
      </view>
      <view class="form-item">
        <view>
          <text class="label" style="color: red">*</text>
          <text class="label">工序名称:</text>
        </view>
        <view v-if="isEdit">
          <text class="iptClass">{{ otherObj.processName }}</text>
        </view>
        <view class="upPickerData" v-else>
          <up-picker-data
            v-model="otherObj.processCode"
            title="请选择工序名称"
            :options="gongxuList"
            valueKey="processCode"
            labelKey="processName"
          >
          </up-picker-data>
        </view>
      </view>
      <view class="form-item">
        <view>
          <text class="label" style="color: red">*</text>
          <text class="label">成品剪线:</text>
        </view>
        <view>
          <up-radio-group
            v-model="otherObj.isFinishedCut"
            placement="column"
            :disabled="isEdit"
          >
            <view class="upRadioGroup">
              <up-radio
                :customStyle="{ marginBottom: '8px' }"
                v-for="(item, index) in radiolist"
                :key="index"
                :label="item.name"
                :name="item.id"
              >
              </up-radio>
            </view>
          </up-radio-group>
        </view>
      </view>
      <view class="form-item">
        <view>
          <text class="label">重量:</text>
        </view>
        <view class="flexCenter" style="flex: 1">
          <input
            class="iptClass textright"
            placeholder="请输入重量"
            v-model="otherObj.weight"
            type="number"
            style="width: 100%"
          />
          <text>kg</text>
        </view>
      </view>
      <view class="form-item">
        <view>
          <text class="label">机台:</text>
        </view>
        <view v-if="isEdit">
          <text class="iptClass">{{ otherObj.machineNumber }}</text>
        </view>
        <view class="upPickerData" v-else>
          <up-picker-data
            v-model="otherObj.machineCode"
            title="请选择机台"
            :options="jitaiList"
            valueKey="machineCode"
            labelKey="machineNumber"
          >
          </up-picker-data>
        </view>
      </view>
      <view class="form-item">
        <view>
          <text class="label">炉管号:</text>
        </view>
        <view v-if="isEdit">
          <text class="iptClass">{{ otherObj.furnaceTubeName }}</text>
        </view>
        <view class="upPickerData" v-else>
          <up-picker-data
            v-model="otherObj.furnaceTubeNo"
            title="请选择炉管号"
            :options="luguanhaoiList"
            valueKey="furnaceTubeNo"
            labelKey="furnaceTubeName"
          >
          </up-picker-data>
        </view>
      </view>
      <view class="form-item">
        <view>
          <text class="label">规格:</text>
        </view>
        <input
          class="iptClass textright"
          placeholder="输入规格"
          v-model.trim="otherObj.dimensions"
          style="width: 100%"
        />
      </view>
    </view>
    <!-- 提交按钮 -->
    <view class="dengjiClass">
      <button @click="onSubmit" class="commonClass">保存</button>
    </view>
  </view>
</template>

<script setup>
import {
  processRecords,
  saomarequest1,
  processProcessDictionary,
  machineallApi,
  machinefurnaceallApi,
} from "./api.js";
import { onCan } from "@/utils/index.js";
const deprops = defineProps(["title"]);
import { useMemberStore } from "@/stores/modules/member";
const memberStore = useMemberStore();
const profile = ref({});
const isEdit = computed(() => deprops.title == "编辑工序");
const otherObj = ref({
  isFinishedCut: 0,
});
//工序名称
const gongxuList = ref([]);
//机台名称
const jitaiList = ref([]);
//炉管号名称
const luguanhaoiList = ref([]);
const radiolist = ref([
  {
    id: 0,
    name: "否",
  },
  {
    id: 1,
    name: "是",
  },
]);
// 点击提交表单
const onSubmit = async () => {
  try {
    if (!profile.value.trackingNo) {
      return uni.showToast({
        icon: "none",
        title: "输入或扫随工单编号二维码",
      });
    }
    if (!otherObj.value.processCode) {
      return uni.showToast({
        icon: "none",
        title: "请选择工序名称",
      });
    }
    if (
      otherObj.value.isFinishedCut != 0 &&
      otherObj.value.isFinishedCut != 1
    ) {
      return uni.showToast({
        icon: "none",
        title: "请选择成品剪线",
      });
    }
    uni.showLoading({
      title: "加载中",
    });
    const res = await processRecords({
      trackingNo: profile.value.trackingNo,
      ...otherObj.value,
    });
    if (res && res.allowUse == false) {
      return uni.showToast({
        icon: "none",
        title: "请选择其他机台或者路管号",
      });
    }
    uni.showToast({
      icon: "success",
      title: otherObj.value.id ? "编辑成功" : "保存成功",
    });
    uni.navigateBack({ delta: 1 });
  } finally {
    uni.hideLoading();
  }
};
const fengzhuang1 = async (value) => {
  profile.value.trackingNo = value;
  const res = await saomarequest1(value);
  if (res) {
    profile.value = res;
    profile.value.trackingNo = value;
  }
};

const handleblur1 = async (result) => {
  const value = result.detail.value;
  if (value) {
    fengzhuang1(value);
  }
};

const handlecan1 = () => {
  onCan(fengzhuang1);
};

const process_ProcessDictionary = async () => {
  try {
    const res = await processProcessDictionary();
    if (res.code == 1) {
      gongxuList.value = res.data.content;
    } else {
      gongxuList.value = [];
    }
  } catch (err) {
    gongxuList.value = [];
  }
};
const machine_furnaceallApi = async (machineId) => {
  try {
    const res = await machinefurnaceallApi(machineId);
    if (res.code == 1) {
      luguanhaoiList.value = res.data.content;
    } else {
      luguanhaoiList.value = [];
    }
  } catch (err) {
    luguanhaoiList.value = [];
  }
};
const machineall_Api = async () => {
  try {
    const res = await machineallApi();
    if (res.code == 1) {
      jitaiList.value = res.data.content;
    } else {
      jitaiList.value = [];
    }
  } catch (err) {
    jitaiList.value = [];
  }
};
//监听工序修改
watch(
  () => otherObj.value.processCode,
  (newval, oldval) => {
    console.log(newval, oldval, "工序");
    const obj = gongxuList.value.find((item) => item.processCode == newval);
    if (obj) {
      otherObj.value.processName = obj.processName;
      otherObj.value.processId = obj.processId;
    }
  }
);
//监听机台修改
watch(
  () => otherObj.value.machineCode,
  (newval, oldval) => {
    console.log(newval, oldval, "机台");
    const obj = jitaiList.value.find((item) => item.machineCode == newval);
    if (obj) {
      otherObj.value.machineNumber = obj.machineNumber;
      otherObj.value.machineId = obj.machineId;
      otherObj.value.furnaceTubeNo = "";
      machine_furnaceallApi(obj.machineId);
    }
  }
);
//监听炉管号修改
watch(
  () => otherObj.value.furnaceTubeNo,
  (newval, oldval) => {
    console.log(newval, oldval, "炉管号");
    const obj = luguanhaoiList.value.find(
      (item) => item.furnaceTubeNo == newval
    );
    if (obj) {
      otherObj.value.furnaceTubeName = obj.furnaceTubeName;
    }
  }
);
onLoad(async () => {
  uni.setNavigationBarTitle({
    title: deprops.title,
  });
  await Promise.all([process_ProcessDictionary(), machineall_Api()]);
  if (isEdit.value) {
    profile.value.trackingNo = memberStore.currenttukutable.trackingNo;
    profile.value.workOrderCode = memberStore.currenttukutable.workOrderCode;
    profile.value.materialCode = memberStore.currenttukutable.materialCode;
    profile.value.materialTypeName =
      memberStore.currenttukutable.materialTypeName;
    profile.value.specificationName =
      memberStore.currenttukutable.specificationName;
    profile.value.heatNo = memberStore.currenttukutable.heatNo;
    otherObj.value.processId = memberStore.currenttukutable.processId;
    otherObj.value.processCode = memberStore.currenttukutable.processCode;
    otherObj.value.processName = memberStore.currenttukutable.processName;
    otherObj.value.isFinishedCut = memberStore.currenttukutable.isFinishedCut;
    otherObj.value.weight = memberStore.currenttukutable.weight;
    otherObj.value.machineId = memberStore.currenttukutable.machineId;
    otherObj.value.machineCode = memberStore.currenttukutable.machineCode;
    otherObj.value.machineNumber = memberStore.currenttukutable.machineNumber;
    await machine_furnaceallApi(otherObj.value.machineId);
    otherObj.value.furnaceTubeNo = memberStore.currenttukutable.furnaceTubeNo;
    otherObj.value.furnaceTubeName =
      memberStore.currenttukutable.furnaceTubeName;
    otherObj.value.dimensions = memberStore.currenttukutable.dimensions;
    otherObj.value.id = memberStore.currenttukutable.id;
    console.log(otherObj.value, "otherObj.value");
    memberStore.currenttukutable = {};
  } else {
    const obj1 = gongxuList.value[0];
    if (obj1) {
      otherObj.value.processCode = obj1.processCode;
      otherObj.value.processName = obj1.processName;
      otherObj.value.processId = obj1.processId;
    } else {
      otherObj.value.processCode = "";
      otherObj.value.processName = "";
      otherObj.value.processId = "";
    }
    const obj2 = jitaiList.value[0];
    if (obj2) {
      otherObj.value.machineCode = obj2.machineCode;
      otherObj.value.machineNumber = obj2.machineNumber;
      otherObj.value.machineId = obj2.machineId;
      await machine_furnaceallApi(obj2.machineId);
    } else {
      otherObj.value.machineCode = "";
      otherObj.value.machineNumber = "";
      otherObj.value.machineId = "";
    }
    const obj3 = luguanhaoiList.value[0];
    if (obj3) {
      otherObj.value.furnaceTubeNo = obj3.furnaceTubeNo;
      otherObj.value.furnaceTubeName = obj3.furnaceTubeName;
    } else {
      otherObj.value.furnaceTubeNo = "";
      otherObj.value.furnaceTubeName = "";
    }
  }
});
</script>

<style lang="scss" scoped>
</style>
